package cc.customer.system.service.impl;

import cc.customer.common.service.impl.BaseService;
import cc.customer.quartz.domain.Job;
import cc.customer.quartz.service.JobService;
import cc.customer.system.dao.SaleTagMapper;
import cc.customer.system.dao.TSeasConfigMapper;
import cc.customer.system.domain.SaleStage;
import cc.customer.system.domain.TSeasConfig;
import cc.customer.system.service.SaleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class SaleServiceImpl extends BaseService<SaleStage> implements SaleService {

   @Autowired
   private TSeasConfigMapper tSeasConfigMapper;

   @Autowired
   private JobService jobService;


   @Override
   public List<SaleStage> selectAll() {
      return super.selectAll();
   }

   @Override
   public int updateSale(SaleStage saleStage){return super.updateNotNull(saleStage);}

   /**查询配置**/
   public  List<TSeasConfig> getAllSeasConfig(){
      try {
         return tSeasConfigMapper.selectAll();
      }catch (Exception e){
         e.printStackTrace();
      }
      return new ArrayList<>();
   }

   /**修改配置**/
   @Override
   @Transactional(propagation = Propagation.REQUIRES_NEW,rollbackFor = Exception.class)
   public int updateSeasConfig(TSeasConfig tSeasConfig) throws Exception{
      try {
         TSeasConfig t = tSeasConfigMapper.selectByPrimaryKey(tSeasConfig.getSgId());
         Job job = jobService.findJob(Long.valueOf(t.getSgJobid()));
         String day = tSeasConfig.getSgDay();
         if (!StringUtils.isEmpty(day)){
            t.setSgDay(day);
            job.setParams(t.getSgDay()+","+t.getSgName());
         }
         String  status = tSeasConfig.getSgStatus();
         if (!StringUtils.isEmpty(status)){
            t.setSgStatus(status);
            job.setStatus(tSeasConfig.getSgStatus());
         }
         int num = tSeasConfigMapper.updateByPrimaryKey(t);
         jobService.updateJob(job);
         return num;
      }catch (Exception e){
         e.printStackTrace();
         throw  new RuntimeException("");
      }
   }
}
